<template>
  <div>
    <br><br><br><br><br><br><br><br><br><br>
    <h2>数字钱包登录</h2>
    <div>
      <el-input style="width: 350px" placeholder="请输入账户名" v-model="username">
        <template slot="prepend">账号：</template>
      </el-input><br><br>
      <el-input style="width: 350px" placeholder="请输入密码" v-model="password">
        <template slot="prepend">密码：</template>
      </el-input><br><br>
      <el-button type="primary" @click="UserLogin">登录</el-button>
      <el-button type="primary" @click="userLogister">注册</el-button>
    </div>
  </div>

</template>

<script>
import axios from "axios";

export default {
  name: 'Login',
  data() {
    return {
      username: '',
      password: '',
    }
  },
  methods: {
    //点击登录按钮执行访问请求
    UserLogin() {
      //发送请求
      this.axios.post("/login", {
        username: this.username,
        password: this.password,
      }).then(res => {
        //setCookie
        console.log(res,'res');
        if (res.data.code == 200) {
          //成功后跳转页面
          this.$router.push({name: 'mainPage'})
        } else {
          alert(res.data.message);
        }
      }).catch(err => {
        console.log(err, 'err');
      })
    },
    userLogister() {
      //发送请求
      this.axios.post("/register", {
        username: this.username,
        password: this.password,
      }).then(res => {
        if (res.data.code == 200) {
          //成功后继续点登录
          alert(res.data.message);
        } else {
          alert(res.data.message);
        }
      })
    },
  }
}
</script>
